package cn.youqu.happy.module.youqu.controller.admin.userInfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userInfo.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userInfo.UserInfoDO;
import cn.youqu.happy.module.youqu.convert.userInfo.UserInfoConvert;
import cn.youqu.happy.module.youqu.service.userInfo.UserInfoService;

@Api(tags = "管理后台 - 用户信息")
@RestController
@RequestMapping("/youqu/user-info")
@Validated
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/create")
    @ApiOperation("创建用户信息")
    @PreAuthorize("@ss.hasPermission('youqu:user-info:create')")
    public CommonResult<Integer> createUserInfo(@Valid @RequestBody UserInfoCreateReqVO createReqVO) {
        return success(userInfoService.createUserInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    @PreAuthorize("@ss.hasPermission('youqu:user-info:update')")
    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody UserInfoUpdateReqVO updateReqVO) {
        userInfoService.updateUserInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-info:delete')")
    public CommonResult<Boolean> deleteUserInfo(@RequestParam("id") Integer id) {
        userInfoService.deleteUserInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-info:query')")
    public CommonResult<UserInfoRespVO> getUserInfo(@RequestParam("id") Integer id) {
        UserInfoDO userInfo = userInfoService.getUserInfo(id);
        return success(UserInfoConvert.INSTANCE.convert(userInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-info:query')")
    public CommonResult<List<UserInfoRespVO>> getUserInfoList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserInfoDO> list = userInfoService.getUserInfoList(ids);
        return success(UserInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-info:query')")
    public CommonResult<PageResult<UserInfoRespVO>> getUserInfoPage(@Valid UserInfoPageReqVO pageVO) {
        PageResult<UserInfoDO> pageResult = userInfoService.getUserInfoPage(pageVO);
        return success(UserInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-info:export')")
    @OperateLog(type = EXPORT)
    public void exportUserInfoExcel(@Valid UserInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserInfoDO> list = userInfoService.getUserInfoList(exportReqVO);
        // 导出 Excel
        List<UserInfoExcelVO> datas = UserInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户信息.xls", "数据", UserInfoExcelVO.class, datas);
    }

}
